package com.xiaoxu.bbs.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xiaoxu.bbs.common.ReturnResults;
import com.xiaoxu.bbs.entity.User;
import com.xiaoxu.bbs.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/register")
    public ReturnResults registerUser(@RequestBody User user, HttpSession session){
        if ("".equals(user.getHead())){
            user.setHead("user_default.jpg");
        }
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUserName,user.getUserName());
        User existUser = userService.getOne(queryWrapper);
        if (existUser == null) {
            userService.save(user);
            User userByName = userService.getOne(queryWrapper);
            Long userId = userByName.getId();
            session.setAttribute("user",userId);
            return ReturnResults.success(userByName);
        }else {
            return ReturnResults.error("用户名虽好，可不要贪杯哦~~");
        }
    }

    @PostMapping("/login")
    public ReturnResults loginUser(@RequestBody User user,HttpSession session){
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUserName,user.getUserName());
        queryWrapper.eq(User::getUserPass,user.getUserPass());
        User existUser = userService.getOne(queryWrapper);
        if (existUser == null) {
            return ReturnResults.error("系统觉得你在逗我，为什么要输错(用户名|密码)! 哎呀，好气啊!");
        }
        session.setAttribute("user",existUser.getId());
        return ReturnResults.success(existUser);
    }

    @PostMapping("/logout")
    public ReturnResults logoutUser(HttpSession session){
        Long userId = (Long) session.getAttribute("user");
        if (userId != null) {
            session.removeAttribute("user");
            return ReturnResults.success("退出顺利，加油，年轻人！");
        }else{
            return ReturnResults.error("后台已经没有该用户信息!");
        }
    }

    @GetMapping("/name/{id}")
    public ReturnResults getUserName(@PathVariable Long id){
        String userName;
        if (id != null) {
            User user = userService.getById(id);
            if (user != null) {
                userName = user.getUserName();
                return ReturnResults.success(userName);
            }
        }
        return ReturnResults.error("请检查一下用户Id是否输入正确");
    }

    @GetMapping("/{userId}")
    public ReturnResults getUserInfo(@PathVariable Long userId){
        if (userId != null) {
            User user = userService.getById(userId);
            if (user != null) {
                return ReturnResults.success(user);
            }
        }
        return ReturnResults.error("请检查一下用户Id师傅输入正确");
    }
}
